/*
 * Copyright (C) 2010 Graham Allan
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.strath.cis.grallan.findbugs.adaptiveranking.analysis;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap;
import edu.strath.cis.grallan.findbugs.adaptiveranking.UserDesignationToPositivesMap.Positive;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.cloud.Cloud.UserDesignation;
import edu.umd.cs.findbugs.gui2.BugDesignationListener;
import edu.umd.cs.findbugs.gui2.IApplicationState;
import edu.umd.cs.findbugs.gui2.IApplicationState.AnalysisChangeListener;

/**
 * @author Graham Allan (grallan@cis.strath.ac.uk)
 */
public class ClassificationOrderMonitor implements BugDesignationListener, AnalysisChangeListener {

	private static final UserDesignationToPositivesMap USER_DESIGNATION_TO_POSITIVES_MAP = new UserDesignationToPositivesMap();
	private final IApplicationState appState;
	private File propertiesFile;
	private List<String> classifications;
	private final String filename;

	public ClassificationOrderMonitor(IApplicationState appState, String filename) {
		this(appState, filename, new ArrayList<String>());
	}

	public ClassificationOrderMonitor(IApplicationState appState, String filename, List<String> cumulativeTruePositives) {
		this.appState = appState;
		this.filename = filename;
		this.classifications = new ArrayList<String>();
		this.classifications.add("0"); // Start at the origin
		this.classifications.addAll(1, cumulativeTruePositives);
	}

	public static void createAndWriteFromList(List<String> cumulativeTruePositives, IApplicationState appState,
			String fileName) {
		new ClassificationOrderMonitor(appState, fileName, cumulativeTruePositives).writeCumulativeTruePositives();
	}

	private void createSaveFile(IApplicationState appState, String filename) {
		File saveFile = appState.getSaveFile();
		File parentFile = saveFile.getParentFile();
		propertiesFile = new File(parentFile.getAbsolutePath() + "/" + filename + ".order");
		propertiesFile.delete();
		try {
			propertiesFile.createNewFile();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void notifyBugDesignated(BugInstance designated) {

		int unclassified = 0;
		for (BugInstance bug : appState.bugCollection()) {
			if (UserDesignation.UNCLASSIFIED.name().equalsIgnoreCase(bug.getUserDesignationKey())) {
				unclassified++;
			}
		}

		int bugsClassified = 0;
		for (BugInstance bug : appState.bugCollection()) {
			if (!UserDesignation.UNCLASSIFIED.name().equalsIgnoreCase(bug.getUserDesignationKey())) {
				bugsClassified++;
			}
		}

		int truePositives = 0;
		for (BugInstance bug : appState.bugCollection()) {
			if (USER_DESIGNATION_TO_POSITIVES_MAP.getPositiveDesignationFromBug(bug) == Positive.TRUE_POSITIVE) {
				truePositives++;
			}
		}

		String strTruePositives = Integer.toString(truePositives);

		classifications.add(strTruePositives);

		writeCumulativeTruePositives();
	}

	private void writeCumulativeTruePositives() {
		createSaveFile(appState, filename);
		try {
			FileWriter fileWriter = new FileWriter(propertiesFile);
			for (String classification : classifications) {
				fileWriter.append(classification + "\n");
			}
			fileWriter.close();

		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public void notifyAnalysisChanged() {
	}
}
